﻿using System;
using System.Web;
using System.Web.Security;
using System.Threading;
using System.Globalization;

namespace MovieCriticsPage
{ 
    /// <summary>
    /// 	The application class provides commonly used functionality and global validation checks.
    /// </summary>
    public class TheApplication : HttpApplication // , ISessionRequest
    {
        const string DEFAULT_LANGUAGE = "en";

        #region Properties
        /// <summary>
        /// 	Gets or sets the language for the current user.
        /// </summary>
        /// <remarks>
        /// 	This property uses the session variable <c>Language</c> (in <see cref = "RisApplication.Session" />).
        /// </remarks>
        /// <value>The language.</value>
        public string Language
        {
            get
            {
                string lang = null;

                if (this.Context != null && this.Context.Request != null)
                {
                    var request = this.Context.Items["Language"];
                    if (request != null)
                        lang = request as string;
                    else
                    {
                        var cookie = this.Context.Request.Cookies["Language"];
                        if (cookie != null)
                            lang = cookie.Value;
                    }
                }

                return lang ?? DEFAULT_LANGUAGE;
            }
            set
            {
                if (this.Context != null && this.Context.Response != null)
                {
                    this.Context.Response.Cookies.Set(new HttpCookie("Language", value));
                }

                this.Context.Items["Language"] = value;
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(value);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(value);
            }
        }

        /// <summary>
        /// 	Gets the application instance for the current Web request.
        /// </summary>
        /// <value>The current.</value>
        public static TheApplication Current
        {
            get { return (TheApplication)HttpContext.Current.ApplicationInstance; }
        }

        public MovieCriticsUser ApplicationUser { get; set; }

        private MovieDatabaseEntities _database;

        public MovieDatabaseEntities Database
        {
            get
            {
                if (this._database == null)
                    this._database = new MovieDatabaseEntities();

                return this._database;
            }
        }

        #endregion


        #region Methods
        public void Logout()
        {
            this.ApplicationUser = null;
            this.Language = DEFAULT_LANGUAGE;

            // Clear user information.
            HttpContext.Current.Session.RemoveAll();
            FormsAuthentication.SignOut();

            // Redirect to home.
            HttpContext.Current.Response.Redirect("~/");
            HttpContext.Current.Response.End();
        }

        void Application_BeginRequest(Object sender, EventArgs e)
        {

            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(Language);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Language);
        }

        #endregion
    }
}
